package com.distributer.message.producer;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;

import com.distributer.message.model.Message;

public class MessageQueue {

	private List<ArrayBlockingQueue<Message>> blockingQueues;
	
	private volatile Map<String, ArrayBlockingQueue<Message>> topicMapping;
	
	private MessageQueue(){
		blockingQueues = new LinkedList<ArrayBlockingQueue<Message>>();
		topicMapping = new ConcurrentHashMap<String, ArrayBlockingQueue<Message>>();
	}
	
	public void addQueue(ArrayBlockingQueue<Message> blockingQueue){
		blockingQueues.add(blockingQueue);
	}
	
	public void addTopicMapping(String topic,ArrayBlockingQueue<Message> queue){
		this.topicMapping.put(topic, queue);
	}
	
	public ArrayBlockingQueue<Message> getQueueByTopic(String topic){
		return this.topicMapping.get(topic);
	}
	
	public boolean exist(String topic){
		return topicMapping.containsKey(topic);
	}
	
	
	private static class MessageQueueInterface{
		
		private static MessageQueue messageQueue;
		
		static{
			messageQueue = new MessageQueue();
		}
		
		private MessageQueueInterface(){}
		
		public static MessageQueue init(){
			return messageQueue;
		}
	}
	
	public static MessageQueue getInterface(){
		return MessageQueueInterface.init();
	}
}
